﻿using Dora.Tools.Enums.Customers;
using Dora.Tools.Insurance.AM.Dto;
using Dora.Tools.Insurance.AYB.Dto;
using Dora.Tools.Insurance.JT.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dora.Tools.Insurance
{
    /// <summary>
    ///  保险投保组装接口数据
    /// </summary>
    public class AssemblyApplyDataHelper
    {
        /// <summary>
        /// 安游保投保接口数据组装
        /// </summary>
        /// <param name="request"></param>
        /// <param name="tradingCode">第三方唯一流水编号</param>
        /// <param name="orgName">组织机构名称</param>
        /// <param name="taxpayerIdentificationNumber">纳税人识别码(统一社会信用代码)</param>
        /// <returns></returns>
        public static ApplyInfoData AssemblyAYBData(RequestInsuranceDto request, string tradingCode, string orgName, string taxpayerIdentificationNumber)
        {
            ApplyInfoData applyData = new ApplyInfoData();
            applyData.base_info = new BaseDataInfo()
            {
                product_id = request.InsuranceProductId,
                biz_order_sn = tradingCode,
                period = request.Period,
                begin_datetime = request.EffectDate.ToString("yyyy-MM-dd 00:00:00"),
                end_datetime = request.ExpiredDate.ToString("yyyy-MM-dd 23:59:59"),
                tour_name = request.TourName,
                tour_code = ""
            };
            //2019-09-25 update:邮箱，手机号没有传空字符串
            #region 2019-12-25 投保人由游客信息改为传公司信息
            //applyData.holder_info = new HolderDataInfo()
            //{
            //    cert_type = request.ApplicantInfo.CertTp,
            //    cert_code = request.ApplicantInfo.CertCode,
            //    name = request.ApplicantInfo.Name,
            //    gender = request.ApplicantInfo.Gender,
            //    birthday = request.ApplicantInfo.Birthday.ToString("yyyy-MM-dd"),
            //    mobile = string.IsNullOrEmpty(request.ApplicantInfo.Mobile) ? "" : request.ApplicantInfo.Mobile,
            //    email = string.IsNullOrEmpty(request.ApplicantInfo.Email) ? "" : request.ApplicantInfo.Email
            //};
            #endregion
            applyData.holder_info = new HolderDataInfo()
            {
                cert_type = "C",
                cert_code = taxpayerIdentificationNumber,
                name = orgName,
                gender = "",
                birthday = "",
                mobile = "",
                email = ""
            };
            List<InsurantDataInfo> dataInfos = new List<InsurantDataInfo>();
            foreach (var item in request.TouristList)
            {
                InsurantDataInfo info = new InsurantDataInfo()
                {
                    name = item.InsuredName,
                    gender = item.Gender,
                    cert_type = item.CertTp,
                    cert_code = item.CertCode,
                    birthday = item.Birthday.ToString("yyyy-MM-dd"),
                    mobile = string.IsNullOrEmpty(item.Mobile) ? "" : item.Mobile,
                    email = string.IsNullOrEmpty(item.Email) ? "" : item.Email
                };
                dataInfos.Add(info);
            }
            applyData.insurant_info = dataInfos;
            return applyData;
        }

        /// <summary>
        /// 安馒投保接口数据组装
        /// </summary>
        /// <param name="request"></param>
        /// <param name="tradingCode">交易号</param>
        /// <param name="userName"></param>
        /// <param name="orgName">组织机构名称</param>
        /// <param name="taxpayerIdentificationNumber">纳税人识别码(统一社会信用代码)</param>
        /// <returns></returns>
        public static AMApplyRequestDto AssemblyAMData(RequestInsuranceDto request, string tradingCode, string userName, string orgName, string taxpayerIdentificationNumber)
        {
            AMApplyRequestDto data = new AMApplyRequestDto();
            data.TradeCode = tradingCode;
            data.UserName = userName;
            data.PlanCode = request.InsuranceProductId;
            data.EffectDate = request.EffectDate.ToString("yyyy-MM-dd");
            data.Period = request.Period;
            data.TourCode = "";
            data.TourName = request.TourName;
            #region 2019-12-25 投保人由游客信息改为传公司信息
            //data.Applicant = new Applicant()
            //{
            //    Name = request.ApplicantInfo.Name,
            //    Gender = request.ApplicantInfo.Gender,
            //    CertTp = request.ApplicantInfo.CertTp,
            //    CertCode = request.ApplicantInfo.CertCode,
            //    Birthday = request.ApplicantInfo.Birthday.ToString("yyyy-MM-dd"),
            //    Mobile = request.ApplicantInfo.Mobile,
            //    Email = request.ApplicantInfo.Email
            //};
            #endregion
            data.Applicant = new Applicant()
            {
                Name = orgName,
                Gender = "",
                CertTp = "O",
                CertCode = taxpayerIdentificationNumber,
                Birthday = "1970-01-01",//时间随便传一个,使用公司的时候
                Mobile = "",
                Email = ""
            };
            List<InsuredInfo> infos = new List<InsuredInfo>();
            foreach (var item in request.TouristList)
            {
                InsuredInfo info = new InsuredInfo()
                {
                    InsuredId = item.InsuredId,
                    InsuredName = item.InsuredName,
                    Gender = item.Gender,
                    CertTp = item.CertTp,
                    CertCode = item.CertCode,
                    Birthday = item.Birthday.ToString("yyyy-MM-dd"),
                    Relation = item.Relation
                };
                infos.Add(info);
            }
            data.SegmentList = infos;
            return data;
        }

        /// <summary>
        /// 江泰投保接口数据组装
        /// </summary>
        /// <param name="request">人员数据</param>
        /// <param name="channel">渠道信息（自定义，双方同步）</param>
        /// <param name="channelComCode">渠道机构代码（江泰提供）</param>
        /// <param name="travelAgencyCode">旅行社代码（江泰提供）</param>
        /// <param name="travelAgencyLicenseCode">旅行社许可证号(org组织信息取)</param>
        /// <param name="channelTradeCode">渠道交易代码（公共字段说明）</param>
        /// <param name="channelBusinessCode">渠道业务代码（江泰提供）</param>
        /// <param name="channelTradeSerialNo">渠道交易流水号</param>
        /// <param name="invoiceTitle">发票抬头</param>
        /// <param name="taxpayerIdentificationNumber">纳税人识别码</param>
        /// <returns></returns>
        public static InsureOrderRequestDto AssemblyJTData(RequestInsuranceDto request, string channel, string channelComCode,
            string travelAgencyCode, string travelAgencyLicenseCode, string channelBusinessCode, string channelTradeSerialNo, string invoiceTitle, string taxpayerIdentificationNumber)
        {
            List<InsureInfos> list = new List<InsureInfos>();
            foreach (var item in request.TouristList)
            {
                InsureInfo info = new InsureInfo()
                {
                    insuredName = item.InsuredName,
                    identifyNumber = item.CertCode,
                    identifyType = item.CertTp == "I" ? 1 : (item.CertTp == "P" ? 3 : 4), //I:身份证,P:护照,O:其他    必填  1 身份证 3护照 4其他
                    birthDay = item.Birthday.ToString("yyyy-MM-dd"),
                    sex = item.Gender
                };
                InsureInfos infos = new InsureInfos()
                {
                    insureInfo = info
                };
                list.Add(infos);
            }
            ChinaTourinsRequest china = new ChinaTourinsRequest()
            {
                channel = channel,
                channelComCode = channelComCode,
                travelAgencyCode = travelAgencyCode,
                travelAgencyLicenseCode = travelAgencyLicenseCode,
                channelTradeCode = "SYN",
                channelBusinessCode = channelBusinessCode,
                channelTradeDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),//渠道交易日期
                channelTradeSerialNo = channelTradeSerialNo,//交易流水号
                channelOperateCode = Guid.NewGuid().ToString(),
                startDate = request.EffectDate.ToString("yyyy-MM-dd") + " 00:00:00",
                endDate = request.ExpiredDate.ToString("yyyy-MM-dd") + " 23:59:59",
                contactName = invoiceTitle, //2019-12-25 投保人由游客信息改为传公司信息 request.ApplicantInfo.Name,
                contactPhone = "", //2019-12-25 投保人由游客信息改为传公司信息 request.ApplicantInfo.Mobile,
                payType = 1,
                travelRoute = request.TourName,
                travelGroupNo = DateTime.Now.ToString("yyyyMMddHHmmss"),
                productCode = request.InsuranceProductId,
                sumQuantity = 1,
                isSendSms = 0,
                invoiceTitle = invoiceTitle,//大社名称
                taxpayerIdentificationNumber = taxpayerIdentificationNumber,//大社统一社会信用代码
                isHighrisk = 0,
                insuredList = list
            };

            InsureOrderRequestDto requestDto = new InsureOrderRequestDto()
            {
                ChinaTourinsRequest = china
            };

            return requestDto;
        }
    }
}
